class Solution:
    def maximumLengthSubstring(self, s: str) -> int:
        n = len(s)
        char_count = {}
        ans = 0
        r = -1
        for l in range(n):
            if l != 0:
                char_count[s[l-1]] = char_count.get(s[l-1],0)-1
                if char_count[s[l-1]] == 0:
                    del char_count[s[l-1]]
            while r < n-1 and char_count.get(s[r+1],0) < 2:
                char_count[s[r+1]] = char_count.get(s[r+1],0)+1
                r+=1
            ans = max(ans,r-l+1)
        return ans
    def maximumLengthSubstring1(self, s: str) -> int:
        ans = 0
        left = 0
        char_map ={}
        for i in range(len(s)):
            char_map[s[i]] = char_map.get(s[i], 0) + 1
            while char_map[s[i]] >2:
                char_map[s[left]] = char_map.get(s[left], 0) - 1
                left+=1
            ans = max(ans,i-left+1)
        return ans

